import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, getInfo, getuserimg } from '@/api/user'
import { resetRouter } from '@/router/index.js'
// import store from '..'
export default {
  namespaced: true,
  state: () => {
    return {
      token: getToken() || '',
      userfrom: {}
    }
  },
  mutations: {
    settoken(state, data) {
      state.token = data
    },
    setuserinfo(state, data) {
      state.userfrom = data
    }

  },
  actions: {
    async dologin(context, data) {
      const datas = await login(data)

      context.commit('settoken', datas.data)
      console.log(datas)
      setToken(datas.data)
    },
    async getuserinfo(context, data) {
      const { data: res } = await getInfo()

      const { data: rs } = await getuserimg(res.userId)
      const obj = { ...res, ...rs }
      context.commit('setuserinfo', obj)
    },
    loginout(context, data) {
      context.commit('setuserinfo', {})
      context.commit('settoken', '')
      removeToken()
      resetRouter()
      // context.commit('menus/clear', [], { root: true })
    }
  },
  getters: {}
}
